package pruebas;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.security.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.DatatypeConverter;
/**
 *
 * @author epindado
 */

public class RecuperarHash {
    public static void main(String args[])
    {
//        try {
//            byte[] bytesOfMessage = "jkhfsadfhaksjfh aksjhf akskasjdhfakf ksjdfkasdhf aksdf bdf gsdbgmsbdfgmbdgmbs dfmgbsdfmbgsmdfgm,sdbfgmsbdfgmbsdfmgbsmb b dsnb dn b,mnb,mb,m dfb,. mdfsb,dfb,,fb ,mfnb,mdfnb ,mdfnb ,mdfnsb,mdnfb,.mfndsb,.mdfnsb,.msndf mjkhfsadfhaksjfh aksjhf akskasjdhfakf ksjdfkasdhf aksdf bdf gsdbgmsbdfgmbdgmbs dfmgbsdfmbgsmdfgm,sdbfgmsbdfgmbsdfmgbsmb b dsnb dn b,mnb,mb,m dfb,. mdfsb,dfb,,fb ,mfnb,mdfnb ,mdfnb ,mdfnsb,mdnfb,.mfndsb,.mdfnsb,.msndf m".getBytes("UTF-8");
//
//            MessageDigest md = MessageDigest.getInstance("SHA-256");
//            byte[] thedigest = md.digest(bytesOfMessage);    
//            System.out.println("Tamaño: " + thedigest.length + ", thedigest: [" + new String(thedigest) + "]");
//            UUEncoder uue = new UUEncoder();
//
//            String thedigest2 = Base64.encode(thedigest);
//            System.out.println("Tamaño: " + thedigest2.length() + ", thedigest: [" + thedigest2 + "]");
//        } catch (NoSuchAlgorithmException ex) {
//            Logger.getLogger(RecuperarHash.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (UnsupportedEncodingException ex) {
//            Logger.getLogger(RecuperarHash.class.getName()).log(Level.SEVERE, null, ex);
//        }
            
            String digestB64 = "";
        try {
            FileInputStream fis = new FileInputStream("C:\\XMLdeskSign.zip");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];

            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
                System.out.println("read " + readNum + " bytes,");
            }
    
            byte[] bytes = bos.toByteArray();
            
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] thedigest = md.digest(bytes);    
            // Codificamos en Base64 para que sea más legible, no en binario
            digestB64 = DatatypeConverter.printBase64Binary(thedigest);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(RecuperarHash.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(RecuperarHash.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            System.out.println(digestB64);
        }
    }
}
